package com.itheima.es;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 实现文档的CRUD
 * @作者 itcast
 * @创建日期 2020/11/29 9:31
 **/
public class EsDemo02 {
    RestHighLevelClient restHighLevelClient;


    @Test
    public void bulkAddDocument() throws IOException {
        // 从数据库中获取的集合数据 要添加到索引库中
        List<User> userList = Arrays.asList(
                new User(1L, "Rose", 18, "1", "Rose同学在传智播客学表演"),
                new User(2L, "Jack", 38, "1", "Jack同学在黑马学JavaEE"),
                new User(3L, "小红", 23, "0", "小红同学在传智播客学唱歌"),
                new User(4L, "小明", 20, "1", "小明同学在黑马学JavaSE"),
                new User(5L, "达摩", 33, "1", "达摩和尚在达摩院学唱歌"),
                new User(6L, "鲁班", 24, "1", "鲁班同学走在乡间小路上"),
                new User(7L, "孙尚香", 26, "0", "孙尚香同学想带阿斗回东吴"),
                new User(8L, "李白", 27, "1", "李白同学在山顶喝着酒唱着歌"),
                new User(9L, "甄姬", 28, "0", "甄姬同学弹奏一曲东风破"),
                new User(10L, "虞姬", 27, "0", "虞姬同学在和项羽谈情说爱")
        );
        // 1. 创建一个批处理的请求
        BulkRequest request = new BulkRequest("user");// 索引库
        for (User user : userList) {
            // 将每一个user  封装成功一个添加文档请求  IndexRequest
            IndexRequest indexRequest = new IndexRequest();
            // 文档的ID
            indexRequest.id(user.getId()+"");
            // 文档的内容
            indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
            request.add(indexRequest);
        }
        // 2. 客户端执行批处理的方法
        BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        // 3. 解析批处理的结果
        System.out.println(bulk.status()); // 处理成功 返回OK
    }


    /**
     * 添加文档
     * @throws IOException
     */
    @Test
    public void addDocument() throws IOException {
        // 准备要存入索引库的数据
        User user = new User(1L,"鲁班",20,"1","鲁班在河道唱着歌");
        // 1. 创建添加文档的请求  IndexRequest
        // POST  xx/_doc/{id}
        // {json内容}
        IndexRequest request = new IndexRequest("user");
        request.id(user.getId()+"");// 设置文档ID
        // 将用户实例序列化成json字符串
        String userJson = JSON.toJSONString(user);
        // 设置到参数中
        request.source(userJson,XContentType.JSON);
        // 2. 执行添加文档的请求
        IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);

        // 3. 解析执行结果  result = created  updated
        System.out.println(index.getResult());
    }

    /**
     * 根据ID查询文档
     * @throws IOException
     */
    @Test
    public void findDocumentById() throws IOException {
        // 1. 创建查看文档的请求  GetRequest
        GetRequest request = new GetRequest("user","1");// 索引库   ID
        // 2. 执行查看文档的请求  .get()
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        // 3. 解析执行结果
        // 获取到的是文档的json字符串
        String userJson = response.getSourceAsString();
        // 将文档的json字符串反序列化成 user 对象
        User user = JSON.parseObject(userJson, User.class);

        System.out.println(user);
    }

    /**
     * 修改文档
     * @throws IOException
     */
    @Test
    public void updateDocument() throws IOException {
        // 1. 创建修改文档的请求  UpdateRequest
        UpdateRequest request = new UpdateRequest("user","1");
        // 参数1:字段名称  参数2:要修改的值
        request.doc("gender","0");
        // 2. 执行修改文档的请求  update
        UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        // 3. 解析执行结果
        System.out.println(update.getResult());
    }

    /**
     * 根据ID删除文档
     * @throws IOException
     */
    @Test
    public void DeleteDocument() throws IOException {
        // 1. 创建修改文档的请求  DeleteRequest
        DeleteRequest request = new DeleteRequest("user","1");
        // 2. 执行修改文档的请求  delete
        DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        // 3. 解析执行结果
        System.out.println(delete.getResult());
    }
    @Before
    public void init(){
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        // 要连接的es地址
                        new HttpHost("192.168.200.150",9200,"http")
//                        new HttpHost("192.168.200.151",9200,"http"),   // 如果是集群的话可以写多个
//                        new HttpHost("192.168.200.152",9200,"http")
                )
        );
    }
    @After
    public void close() throws IOException {
        restHighLevelClient.close();
    }
}
